package com.example.idachuappone.promptly.item;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baidu.location.BDGeofence;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.person.method.AddressMethod;
import com.example.idachuappone.person.method.AddressMethod.SetAddress;
import com.example.idachuappone.promptly.entity.Addresses;
import com.example.idachuappone.utils.CusProSmallBgDialog;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class UpdateAddressActivity extends BaseActivity implements OnClickListener {
	private String addressDingweiname;
	private Addresses addresses;
	private Button btn_back;
	private Button btn_index;
	private CusProSmallBgDialog cusProDialog;
	private EditText et_address_dizi;
	private EditText et_address_xiaoqu;
	private EditText et_name;
	private EditText et_phone;
	private String et_quyu;
	private ImageView img_xiaoqu_dingwei;
	private LinearLayout ll_xiaoqu_dingwei;
	private double locLat;
	private double locLon;
	public LocationClient mLocationClient;
	public BDLocationListener myListener;
	private String paramCoordinate;
	private ProgressBar pb_zairu;
	private int position;

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			UpdateAddressActivity.this.mLocationClient.stop();
			if (location == null) {
				MainToast.show(UpdateAddressActivity.this, "\u65e0\u6cd5\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e", 0);
			} else if (location.getLocType() == 61 || location.getLocType() == 161) {
				UpdateAddressActivity.this.locLat = location.getLatitude();
				UpdateAddressActivity.this.locLon = location.getLongitude();
				UpdateAddressActivity.this.addressDingweiname = location.getStreet();
				UpdateAddressActivity.this.paramCoordinate = new StringBuilder(String.valueOf(String
						.valueOf(UpdateAddressActivity.this.locLat))).append(",")
						.append(String.valueOf(UpdateAddressActivity.this.locLon)).toString();
				UpdateAddressActivity.this.setXiaoQu(UpdateAddressActivity.this.paramCoordinate);
			} else {
				MainToast.show(UpdateAddressActivity.this, "\u65e0\u6cd5\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e", 0);
			}
		}
	}

	public UpdateAddressActivity() {
		this.mLocationClient = null;
		this.myListener = new MyLocationListener();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_update_address);
		this.cusProDialog = new CusProSmallBgDialog(this);
		this.cusProDialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				UpdateAddressActivity.this.cusProDialog.dismiss();
			}
		});
		this.addresses = (Addresses) getIntent().getExtras().getSerializable("address");
		this.position = getIntent().getExtras().getInt("position");
		initView();
		initLocation();
	}

	private void initLocation() {
		this.mLocationClient = new LocationClient(this);
		this.mLocationClient.registerLocationListener(this.myListener);
		this.mLocationClient.setLocOption(setOptionMap());
	}

	private LocationClientOption setOptionMap() {
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setCoorType(BDGeofence.COORD_TYPE_BD09LL);
		option.setScanSpan(LocationClientOption.MIN_SCAN_SPAN);
		option.setIsNeedAddress(true);
		option.setAddrType("all");
		return option;
	}

	private void initView() {
		((TextView) findViewById(R.id.tv_title)).setText("\u4fee\u6539\u670d\u52a1\u5730\u5740");
		this.btn_index = (Button) findViewById(R.id.btn_index);
		this.btn_index.setOnClickListener(this);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.et_address_xiaoqu = (EditText) findViewById(R.id.et_address_xiaoqu);
		this.et_address_xiaoqu.setOnClickListener(this);
		this.et_address_dizi = (EditText) findViewById(R.id.et_address_dizi);
		this.et_name = (EditText) findViewById(R.id.et_name);
		this.et_phone = (EditText) findViewById(R.id.et_phone);
		this.ll_xiaoqu_dingwei = (LinearLayout) findViewById(R.id.ll_xiaoqu_dingwei);
		this.ll_xiaoqu_dingwei.setOnClickListener(this);
		this.img_xiaoqu_dingwei = (ImageView) findViewById(R.id.img_xiaoqu_dingwei);
		this.pb_zairu = (ProgressBar) findViewById(R.id.pb_zairu);
		initData();
	}

	private void initData() {
		this.et_address_xiaoqu.setText(this.addresses.getCommunity());
		this.et_address_dizi.setText(this.addresses.getDoor_number());
		this.et_name.setText(this.addresses.getName());
		this.et_phone.setText(this.addresses.getPhone());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_index /* 2131165213 */:
			if (this.et_name.getText().toString().trim().length() == 0) {
				MainToast.show(this, "\u8bf7\u586b\u5199\u60a8\u7684\u59d3\u540d\uff01", 0);
			} else if (this.et_phone.getText().toString().trim().length() == 0) {
				MainToast.show(this, "\u8bf7\u586b\u5199\u60a8\u7684\u624b\u673a\uff01", 0);
			} else if (this.et_address_xiaoqu.getText().toString().trim().length() == 0) {
				MainToast.show(this, "\u8bf7\u586b\u5199\u60a8\u7684\u5730\u5740\uff01", 0);
			} else if (this.et_address_dizi.getText().toString().trim().length() == 0) {
				MainToast.show(this, "\u8bf7\u586b\u5199\u60a8\u7684\u8be6\u7ec6\u5730\u5740\uff01", 0);
			} else {
				this.cusProDialog.setMsg("\u4fee\u6539\u5730\u5740\u4e2d...");
				this.cusProDialog.show();
				AddressMethod.updateAddress(this, new Addresses(this.addresses.getId(), "", this.et_name.getText()
						.toString(), this.et_phone.getText().toString(), this.et_quyu, this.et_address_xiaoqu.getText()
						.toString(), this.et_address_dizi.getText().toString(), "", ""), this.cusProDialog,
						new SetAddress() {
							@Override
							public void onSuccess(String result) {
								try {
									JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(result));
									if (jsonObject.getInt("code") == 10000) {
										MainToast.show(UpdateAddressActivity.this,
												"\u5730\u5740\u4fee\u6539\u6210\u529f", 0);
										Intent data = new Intent();
										if (UpdateAddressActivity.this.position == 0) {
											data.putExtra("isupdateposition", true);
										}
										UpdateAddressActivity.this.setResult(-1, data);
										UpdateAddressActivity.this.finish();
									} else if (jsonObject.getInt("code") == 300) {
										MainToast.show(UpdateAddressActivity.this, jsonObject.getString("error"), 0);
									}
								} catch (JSONException e) {
									e.printStackTrace();
									MainToast.show(UpdateAddressActivity.this, "\u6682\u65e0\u6570\u636e", 0);
								}
							}

							@Override
							public void onFail() {
								MainToast.show(UpdateAddressActivity.this, UpdateAddressActivity.this.getResources()
										.getString(R.string.netFail), 0);
							}
						});
			}
		case R.id.ll_xiaoqu_dingwei /* 2131165217 */:
			this.img_xiaoqu_dingwei.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.pb_zairu.setVisibility(0);
			this.mLocationClient.start();
		default:
		}
	}

	public void setXiaoQu(String location) {
		NetUtil.get(this, "http://api.map.baidu.com/place/search?query=\u5c0f\u533a&location=" + location
				+ "&radius=500&output=json", 100, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				UpdateAddressActivity.this.et_address_xiaoqu.setTextColor(UpdateAddressActivity.this.getResources()
						.getColor(R.color.black));
				UpdateAddressActivity.this.img_xiaoqu_dingwei.setVisibility(0);
				UpdateAddressActivity.this.pb_zairu
						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					if (jsonObject.getString("status").equals("OK")) {
						JSONArray jsonArray = jsonObject.getJSONArray("results");
						if (jsonArray == null || jsonArray.length() <= 0) {
							UpdateAddressActivity.this.et_address_xiaoqu
									.setText(UpdateAddressActivity.this.addressDingweiname);
							return;
						} else {
							UpdateAddressActivity.this.et_address_xiaoqu.setText(jsonArray.getJSONObject(0).getString(
									"name"));
							return;
						}
					}
					UpdateAddressActivity.this.et_address_xiaoqu.setText(UpdateAddressActivity.this.addressDingweiname);
				} catch (Exception e) {
					e.printStackTrace();
					UpdateAddressActivity.this.et_address_xiaoqu.setText(UpdateAddressActivity.this.addressDingweiname);
				}
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				UpdateAddressActivity.this.et_address_xiaoqu.setTextColor(UpdateAddressActivity.this.getResources()
						.getColor(R.color.black));
				UpdateAddressActivity.this.img_xiaoqu_dingwei.setVisibility(0);
				UpdateAddressActivity.this.pb_zairu
						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				UpdateAddressActivity.this.et_address_xiaoqu.setText(UpdateAddressActivity.this.addressDingweiname);
				MainToast.show(UpdateAddressActivity.this,
						UpdateAddressActivity.this.getResources().getString(R.string.netFail), 0);
			}
		});
	}
}
